<?

class holiday_top_deals {


	var $table_fields = array(
		'reference_id',
		'tour_op',
		'destination_airport',
		'departure_airport',
		'departure_date',
		'return_date',
		'nights',
		'board_basis',
		'price',
		'hotel_tree_id'
		);

// Function // Get top holiday deals
function get_top_deals($params=''){

	$params = in($params);

	if(!empty($params['destination_airports_filter_in'])){
		$where .= " AND FIND_IN_SET(destination_airport,'".$airports_filter_in."') ";
	}
	if(!empty($params['destination_airports_filter_out'])){
		$where .= " AND !FIND_IN_SET(destination_airport,'".$airports_filter_out."') ";
	}
	if(!empty($params['limit'])){
		$limit_sql .= " LIMIT ".$params['limit']." ";
	}
	if(!empty($params['to_filter_in'])){
		$where .= " AND FIND_IN_SET(tour_op,'".$to_filter_in."') ";
	}
	if(!empty($params['to_filter_out'])){
		$where .= " AND !FIND_IN_SET(tour_op,'".$to_filter_out."') ";
	}


	$sql = "SELECT reference_id, date, tour_op, departure_date, return_date, departure_airport, destination_airport, nights, price, hotel_tree_id, board_basis
	FROM holiday_top_deals
	WHERE board_basis != 'F0'
	AND price < 800
	AND departure_date BETWEEN ".date('ymd',strtotime("+2 days"))." AND ".date('ymd',strtotime("+17 days"))."
	".$where."
	GROUP BY destination_airport, nights, departure_airport, departure_date
	ORDER BY price
	".$limit_sql." ";


		return apply_query($sql, $numrows, $params['cache']);

}




// Function // Get top holiday deals by destination
function get_top_deals_destination($params=''){

	$params = in($params);

	if(!empty($params['destination_airport'])){
		$where .= " AND destination_airport = '".$params['destination_airport']."' ";
	}

	if(!empty($params['limit'])){
		$limit_sql .= " LIMIT ".$params['limit']." ";
	}



	$sql = "SELECT reference_id, date, tour_op, departure_date, return_date, departure_airport, destination_airport, nights, price, hotel_tree_id, board_basis
	FROM holiday_top_deals
	WHERE board_basis != 'F0'
	AND price < 800
	AND departure_date BETWEEN ".date('ymd',strtotime("+2 days"))." AND ".date('ymd',strtotime("+21 days"))."
	".$where."
	GROUP BY destination_airport, nights, departure_airport, departure_date
	#ORDER BY price
	".$limit_sql." ";


		return apply_query($sql, $numrows, $params['cache']);

}




// Function // Find same deal
function get_top_deal($reference_id){
	
	if(!empty($reference_id)){

		$reference_id = in($reference_id);
		
		$sql = "SELECT reference_id
		FROM holiday_top_deals
		WHERE reference_id = '".$reference_id."' ";
		
		return apply_query($sql);
		
	}

}




// FUNCTION // Insert top deal 
function insert_top_deal($params){



	if(!$this->get_top_deal($params['reference_id'])){		

		$params = array_remove_empty($params);
		$params = pair_array_data($params, $this->table_fields);
		
		$params = in($params);

		$sql = "INSERT INTO holiday_top_deals
		SET	".make_set_clause($params)."   ";
		
		$result = mysql_query($sql) or die (mysql_error());
		
		return mysql_insert_id();
	
	}


}


// FUNCTION // Delete related top deals
function delete_related_top_deals_holidays($params){

	
	if($params['nights']>1){
		
		$sql = "DELETE
		FROM holiday_top_deals
		WHERE destination_airport = '".$params['destination_airport']."'
		AND departure_airport = '".$params['departure_airport']."'
		AND departure_date = '".$params['departure_date']."'
		AND nights = '".$params['nights']."' 
		AND board_basis != 'F0' ";
		
		$result = mysql_query($sql) or die (mysql_error());
	
	}	

}


// FUNCTION // Delete a top deal
function delete_top_deals_holiday($reference_id){

	if(!empty($reference_id)){
	
		$params = in($params);
		
		$sql = "DELETE
		FROM holiday_top_deals
		WHERE reference_id  = '".$reference_id."' ";
	
		$result = mysql_query($sql) or die (mysql_error());
	
	}
}

}

?>